package array.removeElement;

public class N977_SortedSquares {
    public int[] sortedSquares(int[] nums) {
        int[] result = new int[nums.length];
        int left = 0, right = nums.length - 1, i = right;
        while (left <= right) {
            if (nums[left] * nums[left] > nums[right] * nums[right]) {
                result[i--] = nums[left] * nums[left];
                left++;
            } else {
                result[i--] = nums[right] * nums[right];
                right--;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        N977_SortedSquares solution = new N977_SortedSquares();
        int[] nums = {-10,5,6};
        for (int i : solution.sortedSquares(nums)) {
            System.out.print(i + " ");
        }
    }
}
